package com.ruoyi.zy.controller;

import java.util.List;

import cn.hutool.extra.pinyin.PinyinUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.zy.domain.VmeBencao;
import com.ruoyi.zy.service.IVmeBencaoService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 中药本草Controller
 * 
 * @author ruoyi
 * @date 2024-03-29
 */
@Controller
@RequestMapping("/zy/bencao")
public class VmeBencaoController extends BaseController
{
    private String prefix = "zy/bencao";

    @Autowired
    private IVmeBencaoService vmeBencaoService;

    @RequiresPermissions("zy:bencao:view")
    @GetMapping()
    public String bencao()
    {
        return prefix + "/bencao";
    }


    /**
     * 查询中药本草列表
     */
    @RequiresPermissions("zy:bencao:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(VmeBencao vmeBencao)
    {
        startPage();
        List<VmeBencao> list = vmeBencaoService.selectVmeBencaoList(vmeBencao);
        return getDataTable(list);
    }

    /**
     * 导出中药本草列表
     */
    @RequiresPermissions("zy:bencao:export")
    @Log(title = "中药本草", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(VmeBencao vmeBencao)
    {
        List<VmeBencao> list = vmeBencaoService.selectVmeBencaoList(vmeBencao);
        ExcelUtil<VmeBencao> util = new ExcelUtil<VmeBencao>(VmeBencao.class);
        return util.exportExcel(list, "中药本草数据");
    }

    /**
     * 新增中药本草
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存中药本草
     */
    @RequiresPermissions("zy:bencao:add")
    @Log(title = "中药本草", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(VmeBencao vmeBencao)
    {
        // 转换整个字符串到拼音，并且去除声调
        String pinyinStr = PinyinUtil.getPinyin(vmeBencao.getName());
        String pinyinWithoutTone = PinyinUtil.getPinyin(pinyinStr, "");
        vmeBencao.setPingYing(pinyinWithoutTone);
        return toAjax(vmeBencaoService.insertVmeBencao(vmeBencao));
    }

    /**
     * 修改中药本草
     */
    @RequiresPermissions("zy:bencao:edit")
    @GetMapping("/edit/{bencaoId}")
    public String edit(@PathVariable("bencaoId") Long bencaoId, ModelMap mmap)
    {
        VmeBencao vmeBencao = vmeBencaoService.selectVmeBencaoByBencaoId(bencaoId);
        mmap.put("vmeBencao", vmeBencao);
        return prefix + "/edit";
    }

    /**
     * 修改保存中药本草
     */
    @RequiresPermissions("zy:bencao:edit")
    @Log(title = "中药本草", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(VmeBencao vmeBencao)
    {
        // 转换整个字符串到拼音，并且去除声调
        String pinyinStr = PinyinUtil.getPinyin(vmeBencao.getName());
        String pinyinWithoutTone = PinyinUtil.getPinyin(pinyinStr, "");
        vmeBencao.setPingYing(pinyinWithoutTone);
        return toAjax(vmeBencaoService.updateVmeBencao(vmeBencao));
    }

    /**
     * 删除中药本草
     */
    @RequiresPermissions("zy:bencao:remove")
    @Log(title = "中药本草", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(vmeBencaoService.deleteVmeBencaoByBencaoIds(ids));
    }
}
